﻿namespace TimeManagement.DataAccess.Configurations
{
    using Models;
    using System.ComponentModel.DataAnnotations.Schema;
    using System.Data.Entity.Infrastructure.Annotations;
    using System.Data.Entity.ModelConfiguration;

    public class UserGroupConfiguration : EntityTypeConfiguration<UserGroup>
    {
        public UserGroupConfiguration()
        {
            ToTable("UserGroups", "tms");

            Property(ug => ug.UserGroupId).IsRequired()
                .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
            Property(ug => ug.UserGroupName).IsRequired()
                .HasMaxLength(64)
                .HasColumnAnnotation("Index", new IndexAnnotation(
                    new IndexAttribute("UserGroup") { IsUnique = true }
                    ));
            Property(ug => ug.Roles).IsRequired()
                .HasMaxLength(512);

            HasMany(u => u.Users)
                .WithOptional()
                .HasForeignKey(ug => ug.UserGroupId);
        }
    }
}
